package com.linkdood.app.utils;

import com.linkdood.app.domain.*;
import com.linkdood.app.service.ddio.DdioOrgSyncResponseBody;
import com.linkdood.app.service.ddio.DdioUserSyncResponseBody;
import org.apache.commons.lang3.StringUtils;

/**
 * @author cfj
 */
public class DdioConverUtil {

    public static Organization convert(DoodOrganization org) {
        Organization organization = new Organization();
        if (org != null) {
            organization.setDepartLevel(org.getDepartLevel());
            organization.setEnterpriseId(org.getEnterpriseID());
            organization.setIsHidden(org.getIsHidden());
            organization.setLeaf(org.getLeaf());
            organization.setOrderNum(org.getOrderNum());
            organization.setOrgCode(org.getOrgCode());
            organization.setOrgId(org.getOrgID());
            organization.setOrgName(org.getOrgName());
            organization.setParentOrgId(org.getParentOrgID());
            organization.setRemark(org.getRemark());
        }
        return organization;
    }

    public static Organization convertSyncOrg(Organization organization, DdioOrgSyncResponseBody.DdioSyncOrg org) {
        if (org != null) {
            if (org.getLeaf() != null) {
                organization.setLeaf(org.getLeaf());
            }
            if (StringUtils.isNotBlank(org.getOrgCode())) {
                organization.setOrgCode(org.getOrgCode());
            }
            if (StringUtils.isNotBlank(org.getOrgName())) {
                organization.setOrgName(org.getOrgName());
            }
            if (org.getParentOrgId() != null) {
                organization.setParentOrgId(String.valueOf(org.getParentOrgId()));
            }
            if (StringUtils.isNotBlank(org.getRemark())) {
                organization.setRemark(org.getRemark());
            }
        }
        return organization;
    }

    public static UserData convertUserData(DoodUser doodUser) {
        UserData userData = new UserData();
        userData.setAccount(doodUser.getAccount());
        userData.setBirthday(doodUser.getBirthday());
        userData.setDdioVersion(doodUser.getDdioVersion());
        userData.setEmails(doodUser.getEmails());
        userData.setEnterpriseId(doodUser.getEpId());
        userData.setEntExtend(doodUser.getEntExtend());
        userData.setFirstPinYin(PinYinUtil.getFirstPinyin(doodUser.getName()));
        userData.setOrgId(doodUser.getOrgID());
        userData.setRealName(doodUser.getRealname());
        userData.setPortraitUrl(doodUser.getPortraitURL());
        userData.setName(doodUser.getName());
        userData.setPhoneNums(doodUser.getPhoneNums());
        userData.setSex(doodUser.getSex());
        userData.setRoleId(doodUser.getRoleID());
        return userData;
    }

    public static UserData convertSyncUser(UserData userData, DdioUserSyncResponseBody.DdioSyncUser syncUser) {
        if (syncUser != null) {
            userData.setAccount(syncUser.getAccount());
            if(syncUser.getChangeVersion() != null){
                userData.setDdioVersion(syncUser.getChangeVersion().toString());
            }
            // 没有信息
            userData.setEnterpriseId("");
            userData.setRoleId(null);

            userData.setEntExtend(syncUser.getEntExtend());
            userData.setFirstPinYin(PinYinUtil.getFirstPinyin(syncUser.getName()));
            userData.setOrgId(syncUser.getOrgID());
            userData.setRealName(syncUser.getRealname());
            userData.setPortraitUrl(syncUser.getPortraitUrl());
            userData.setName(syncUser.getName());
            userData.setPhoneNums(syncUser.getPhoneNum());
            if(StringUtils.isNotBlank(syncUser.getSex())){
                userData.setSex(Integer.parseInt(syncUser.getSex()));
            }
        }
        return userData;
    }
}
